import requests
from bs4 import BeautifulSoup
import csv

url = "https://movie.douban.com/top250?start={}&filter="
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}

def get_movie_info(movie_url):
    response = requests.get(movie_url, headers=headers)
    soup = BeautifulSoup(response.text, "html.parser")
    movie = {}

def get_movie_info(movie_url):
    response = requests.get(movie_url, headers=headers)
    soup = BeautifulSoup(response.text, "html.parser")
    movie = {}
    title_tag = soup.find("span", property="v:itemreviewed")
    if title_tag:
        movie["title"] = title_tag.text
    else:
        movie["title"] = "未知"

        year_tag = soup.find("span", class_="year")
        if year_tag:
            movie["year"] = year_tag.text.strip("()")
        else:
            movie["year"] = "未知"

        director_tags = soup.find_all("a", rel="v:directedBy")
        movie["director"] = [a.text for a in director_tags]

        cast_tags = soup.find_all("a", rel="v:starring")
        movie["cast"] = [a.text for a in cast_tags]

        duration_tag = soup.find("span", property="v:runtime")
        if duration_tag:
            movie["duration"] = duration_tag.text
        else:
            movie["duration"] = "未知"

        return movie

    movie["title"] = soup.find("span", property="v:itemreviewed").text
    movie["year"] = soup.find("span", class_="year").text.strip("()")
    movie["director"] = [a.text for a in soup.find_all("a", rel="v:directedBy")]
    movie["cast"] = [a.text for a in soup.find_all("a", rel="v:starring")]
    movie["duration"] = soup.find("span", property="v:runtime").text
    return movie

with open("top250-2.csv", "w", newline="", encoding="utf-8") as csvfile:
    fieldnames = ["title", "year", "director", "cast", "duration"]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()

    for i in range(0, 250, 25):
        response = requests.get(url.format(i), headers=headers)
        soup = BeautifulSoup(response.text, "html.parser")
        movie_links = [a["href"] for a in soup.find_all("a", class_="")]

        for link in movie_links:
            movie = get_movie_info(link)
            writer.writerow(movie)